if(!jsx3.lang.Class.forName("tibco.admin.deployment.sa.Details")) {

    jsx3.lang.Package.definePackage("tibco.admin.deployment.sa", function (deploy) {
    });

    jsx3.lang.Class.defineClass(
            'tibco.admin.deployment.sa.Details', // full name of the class to create
            jsx3.lang.Object,
            [tibco.admin.plugin.PluginContainerInterface],
            function (plugin) {
                plugin.prototype.buttonPlugins = new Object();
                plugin.prototype.buttonsToLoad = new Object();
                plugin.instance = null;
                plugin.prototype.titleBlock = null;

                plugin.prototype.block = null;

                plugin.prototype.lastContentPane = null;
                plugin.prototype.lastGeneralSettingContentPane = null;

                // true, if general setting is visible; false, if Service Unit is visible
                plugin.prototype.generalSettingsVisible = true;

                plugin.getInstance = function() {
                    if(!this.instance) {
                        this.instance = new tibco.admin.deployment.sa.Details();
                    }
                    return this.instance;
                }

                plugin.prototype.onAfterGetPlugins = function() {
                    var iter = this.getPluginList().iterator();
                    var first = this.buttonPane.getButtonPane().getChildren().length == 0;
                    var loaded = false;
                    while(iter.hasNext()) {
                        try {
                            var obj = iter.next();
                            var button = this.buttonPane.addButton(obj.getButtonText());
                            if(first) {
                                first = false;
                                obj.loadComponent(button.getContentPane());
                                loaded = true;
                            }
                            else {
                                this.buttonsToLoad['button' + obj.getButtonText()] = obj;
                            }
                            this.buttonPlugins['button' + obj.getButtonText()] = obj;
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }

                    // subscribe event
                    var me = this;
                    this.buttonPane.subscribe(com.gix.gui.LeftSelectPane.BUTTON_SELECTED, me, me.loadButton)

                    // set last content pane to the first pane
                    this.lastContentPane = this.buttonPane.getContentPane().getChild(0);
                }

                plugin.prototype.loadButton = function(objEvent) {
                    var name = objEvent.target.getText();
                    var obj = this.buttonsToLoad['button' + name];
                    var contentPane = objEvent.target.getContentPane();
                    var loaded = false;
                    if(obj) {
                        if(this.lastContentPane) {
                            this.unsubscribeAllInPane(this.lastContentPane)
                        }
                        obj.loadComponent(contentPane);
                        loaded = true;
                        delete this.buttonsToLoad['button' + name];
                        this.currentPlugin = obj;
                        this.lastContentPane = contentPane;
                    }
                    else {
                        obj = this.buttonPlugins['button' + name];
                        if(obj) {
                            obj.buttonSelected();
                        }
                        this.subscribeAllInPane(contentPane);
                    }
                }

                plugin.prototype.setupPlugin = function(childPlugin) {
                }

                plugin.prototype.beginEdit = function(objPlugin) {
                    try {
                        var me = this;
                        this.handler.beginEdit(me);
                        if(jsx3.Class.forName("tibco.admin.deployment.sa.ServiceAssemblyInterface").isInstance(objPlugin)) {
                            var button = this.findButton(objPlugin.getButtonText());
                            if(button) {
                                this.buttonPane.setEditMode(button, true);
                            }
                        }
                        else if(objPlugin == this.SUDetail) {
                            this.SUList.beginEdit();
                        }
                    }
                    catch(ex) {
                        jsx3.log(ex);
                        tibco.admin.Main.logException(ex);
                    }
                }

                plugin.prototype.findButton = function(strName) {
                    var button = null;
                    try {
                        if(this.buttonPane) {
                            var buttons = this.buttonPane.getButtonPane().getChildren();
                            for(var name in buttons) {
                                var btn = buttons[name];
                                if(btn.getText() == strName) {
                                    button = btn;
                                    break;
                                }
                            }
                        }
                    }
                    catch(ex) {
                        // do nothing
                    }
                    return button
                }

                plugin.prototype.endEdit = function(objPlugin) {
                    try {
                        if(jsx3.Class.forName("tibco.admin.deployment.sa.ServiceAssemblyInterface").isInstance(objPlugin)) {
                            var button = this.findButton(objPlugin.getButtonText());
                            if(button) {
                                this.buttonPane.setEditMode(button, false);
                            }
                        }
                        else if(objPlugin == this.SUDetail) {
                            this.SUList.endEdit();
                        }
                        if(!this.isInEditMode()) {
                            var me = this;
                            this.handler.endEdit(me);
                        }
                    }
                    catch(ex) {
                        jsx3.log(ex);
                        tibco.admin.Main.logException(ex);
                    }
                }

                plugin.prototype.initPlugin = function() {
                }

                plugin.prototype.loadMyDetails = function() {
                    if(this.getPluginList().size() == 0) {
                        var preLoad = new jsx3.util.List(0);
                        if(!tibco.admin.deployment.sa.updateSA || !tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/deployment/sa/updateSA/UpdateServiceAssembly.js", "tibco.admin");
                                preLoad.add(tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.deployment.sa.updateSA.UpdateServiceAssembly, preLoad.size());
                        }

                        if(!tibco.admin.deployment.sa.sRsrcList || !tibco.admin.deployment.sa.sRsrcList.SharedResource) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/deployment/sa/sRsrcList/SharedResource.js", "tibco.admin");
                                preLoad.add(tibco.admin.deployment.sa.sRsrcList.SharedResource, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.deployment.sa.sRsrcList.SharedResource, preLoad.size());
                        }

                        if(!tibco.admin.deployment.sa.topicDef || !tibco.admin.deployment.sa.topicDef.TopicDef) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/deployment/sa/topicDef/TopicDef.js", "tibco.admin");
                                preLoad.add(tibco.admin.deployment.sa.topicDef.TopicDef, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.deployment.sa.topicDef.TopicDef, preLoad.size());
                        }

                        if(!tibco.admin.deployment.sa.subVar || !tibco.admin.deployment.sa.subVar.SubVar) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/deployment/sa/subVar/subVar.js", "tibco.admin");
                                preLoad.add(tibco.admin.deployment.sa.subVar.SubVar, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.deployment.sa.subVar.SubVar, preLoad.size());
                        }

                        if(!tibco.admin.environments.logCfg || !tibco.admin.environments.logCfg.LogCfg) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/environments/logCfg/LogCfg.js", "tibco.admin");
                                preLoad.add(tibco.admin.environments.logCfg.LogCfg, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.environments.logCfg.LogCfg, preLoad.size());
                        }

                        if(!tibco.admin.deployment.sa.svcList || !tibco.admin.deployment.sa.svcList.SvcList) {
                            try {
                                tibco.admin.Main.loadClass("tibco/admin/deployment/sa/svcList/svcList.js", "tibco.admin");
                                preLoad.add(tibco.admin.deployment.sa.svcList.SvcList, preLoad.size());
                            }
                            catch(ex) {
                                tibco.admin.Main.logException(ex);
                            }
                        }
                        else {
                            preLoad.add(tibco.admin.deployment.sa.svcList.SvcList, preLoad.size());
                        }

                        this.loadPlugins("tibco.admin.deployment.sa.ServiceAssemblyInterface", preLoad);
                    }
                    else {
                        this.onAfterGetPlugins();
                    }
                };

                plugin.prototype.swithPane = function(objEvent, objTarget) {
                    var paneLower = this.block.getDescendantOfName("paneLower");
                    var paneGeneralSettings = paneLower.getDescendantOfName("lspGeneralSettings_40");
                    var paneServiceUnits = paneLower.getDescendantOfName("paneServiceUnits_40");
                    var paneServiceUnitList = paneServiceUnits.getDescendantOfName("paneServiceUnitList_40");
                    var targetName = objTarget.getName();
                    switch(targetName) {
                        case "btnGeneralSettings_40":
                            if(this.generalSettingsVisible) return;
                            this.generalSettingsVisible = true;

                            if(this.lastContentPane) {
                                this.unsubscribeAllInPane(this.lastContentPane)
                            }

                            this.subscribeAllInPane(this.lastGeneralSettingContentPane);
                            this.lastContentPane = this.lastGeneralSettingContentPane;

                            paneServiceUnits.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                            paneGeneralSettings.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                            this.buttonPlugins["button" + this.buttonPane.getSelectedButton().getText()].buttonSelected();
                            break;

                        case"btnServiceUnits40":    // load service Units
                            if(!this.generalSettingsVisible) return;
                            this.generalSettingsVisible = false;

                            this.lastGeneralSettingContentPane = this.lastContentPane;

                            if(this.lastContentPane) {
                                this.unsubscribeAllInPane(this.lastContentPane)
                            }

                            this.lastContentPane = paneServiceUnitList;
                            paneGeneralSettings.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                            paneServiceUnits.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                            if(paneServiceUnitList.getChildren().length == 0) {
                                var me = this;
                                this.SUList = paneServiceUnitList.load("tibco/admin/deployment/sa/su/sulist/ui/suList.xml", true).handler;
                                this.SUDetail = paneServiceUnits.getDescendantOfName("paneServiceUnitDetail_40").load("tibco/admin/deployment/sa/su/ui/suDetail.xml", true).handler;
                                this.SUList.handler = me;
                                this.SUDetail.handler = me;
                            }
                            else {
                                this.subscribeAllInPane(paneServiceUnitList);
                                this.SUList.callgetAllServiceUnitsSummary();
                                this.SUDetail.onRefresh();
                            }
                            break;
                    }
                }


                // call subscribe once for all children of specified pane, not include the pane self.
                plugin.prototype.subscribeAllInPane = function(pane) {
                    // subscribe events for the active pane
                    if(pane.getChildren().length > 0 && pane.getChild(0).handler && pane.getChild(0).handler.subscribeAll) {
                        pane.getChild(0).handler.subscribeAll();
                    }
                }

                // call unsubscribe once for all children of specified pane, not include the pane self.
                plugin.prototype.unsubscribeAllInPane = function(pane) {
                    // subscribe events for the active pane
                    try {
                        if(pane.getChildren().length > 0 && pane.getChild(0).handler && pane.getChild(0).handler.unsubscribeAll) {
                            pane.getChild(0).handler.unsubscribeAll();
                        }
                    }
                    catch(ex) {
                    }
                }

                plugin.prototype.serviceUnitSelected = function(objEvent) {
                    this.block.getDescendantOfName("lblSelectedServiceUnit_40").setText(objEvent.name, true);
                    //TODO: show/hide tabs based on selection
                }

                plugin.prototype.subscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.subscribe(tibco.admin.deployment.sa.su.sulist.EventID.SERVICEUNIT_ROW_SELECTED, me, me.serviceUnitSelected);
                };

                plugin.prototype.unsubscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.sa.su.sulist.EventID.SERVICEUNIT_ROW_SELECTED, me);
                }

                /*
                * MUST appear in "onAfterDeserialize" textarea.
                * @Param: objJSX {jsx3.gui.Object} the root JSX Object of current Component.
                */
                plugin.onAfterDeserialize = function(objJSX) {
                    var instance = plugin.getInstance();
                    instance.block = objJSX;
                    instance.block.handler = instance;
                    instance.generalSettingsVisible = true;
                    instance.buttonPane = objJSX.getDescendantOfName("lspGeneralSettings_40");
                    instance.loadMyDetails();

                }

                plugin.prototype.getPluginInitArgs = function() {
                    return ["ServiceAssembly","true"];
                }

                plugin.prototype.getComponentPath = function() {
                    return  "tibco/admin/deployment/sa/component.xml"
                }

                plugin.prototype.isInEditMode = function() {
                    return (this.isEditMode || this.childIsInEditMode() || (this.SUDetail && this.SUDetail.isInEditMode()));
                }

                plugin.prototype.allowComponentUnload = function(target, fctnYes, fctnNo, args) {
                    var me = this;
                    var obj = new Object();
                    obj.iter = this.getPluginList().iterator();
                    obj.target = target;
                    obj.fctnYes = fctnYes;
                    obj.fctnNo = fctnNo;
                    obj.args = args;
                    if(this.SUDetail) {
                        this.SUDetail.allowComponentUnload(me, me.childAllowsUnload, me.childDisallowsUnload, obj);
                    }
                    else {
                        if(obj.iter.hasNext()) {
                            obj.iter.next().allowComponentUnload(me, me.childAllowsUnload, me.childDisallowsUnload, obj);
                        }
                        else {
                            this._allowComponentUnload(target, fctnYes, fctnNo, args);
                        }
                    }
                };

                plugin.prototype.unload = function() {
                    if(this.SUDetail) {
                        this.SUDetail.unload();
                    }
                    this.onAfterContainerUnloaded();
                }

                plugin.prototype.onRefresh = function() {
                    if(this.generalSettingsVisible) {
                        //refresh current plugin
                        this.buttonPane.getSelectedButton().select();
                    }
                    else {
                        //refresh SUDetails and SUList
                        this.SUList.callgetAllServiceUnitsSummary();
                        this.SUDetail.onRefresh();
                    }
                }
            }
            );
}